﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FE.Common;
using FE.Service.Contract;
using FE.Shared;
using FE.Shared.Enum;

namespace fe.pdf.convert.jobs.webapp.Controllers
{
    public class ServerFilePathController : Controller
    {
        private readonly IAppService _appService;

        public ServerFilePathController(IAppService appService)
        {
            _appService = appService;
        }

        public IActionResult Index(HttpResult result)
        {
            if (result?.ResultCode > 0)
            {
                ViewBag.Msg = result.ErrorMsg;
                ViewBag.ResultCode = result.ResultCode;
            }

            var app = _appService.ReadAppSettings();

            if (app == null || app.ServerPathSettings == null || app.ServerPathSettings?.FileMode <= 0)
            {
                return View();
            }

            ViewBag.FileMode = app.ServerPathSettings.FileMode;

            return View(app.ServerPathSettings);
        }

        public IActionResult Update(ServerPathSettings settings)
        {
            ViewBag.FileMode = 0;

            if (!ModelState.IsValid)
            {
                return View("Index", settings);
            }

            var result = new HttpResult
            {
                ResultCode = 0
            };

            if (settings == null || settings.FileMode <= 0)
            {
                return ReturnActionResult(result);
            }

            try
            {
                var curSettings = MapSettingsAndVerifySettings(settings);
                //todo 这里可以校验,如果没有修改,则不需要保存文件
                var ok = _appService.SaveAppSettings(settings);
                result.ResultCode = ok ? ResultCode.Success : ResultCode.Failed;
                ViewBag.FileMode = settings.FileMode;
            }
            catch (Exception e)
            {
                result.ResultCode = ResultCode.Failed;
                result.ErrorMsg = e.Message;
            }

            return ReturnActionResult(result);
        }

        private IActionResult ReturnActionResult(HttpResult result)
        {
            return RedirectToAction(nameof(Index), "ServerFilePath", result);
        }

        private ServerPathSettings MapSettingsAndVerifySettings(ServerPathSettings settings)
        {
            var curSettings = new ServerPathSettings();

            if (settings.FileMode == 1)
            {
                //先验证
                bool verify = ValidateFolderPath(settings);

                if (verify)
                {
                    //文件夹模式
                    curSettings = new ServerPathSettings
                    {
                        FileMode = settings.FileMode,
                        FileAttachmentRootPath = settings.FileAttachmentRootPath,
                        PdfGeneratedServerPath = settings.PdfGeneratedServerPath
                    };
                }
            }
            else
            {
                bool verify = ValidateSftpPath(settings);

                //sftp模式
                curSettings = new ServerPathSettings
                {
                    FileMode = settings.FileMode,
                    SftpFileAttachmentRootPath = settings.SftpFileAttachmentRootPath,
                    SftpPdfGeneratedServerPath = settings.SftpPdfGeneratedServerPath
                };
            }

            return curSettings;
        }

        private bool ValidateFolderPath(ServerPathSettings settings)
        {
            var rootPath = ValidateFilePath(settings.FileAttachmentRootPath);
            var pdfPath = ValidateFilePath(settings.PdfGeneratedServerPath);

            return rootPath && pdfPath;
        }

        private bool ValidateSftpPath(ServerPathSettings settings)
        {
            //尝试获取PDF目录文件
            SftpHelper.ConnTest(settings.SftpPdfGeneratedServerPath);

            //尝试获取目录文件
            SftpHelper.ConnTest(settings.SftpFileAttachmentRootPath);

            return true;
        }

        private bool ValidateFilePath(string path)
        {
            bool ok = false;
            try
            {
                ok = FileHelper.TestPath(path);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return true;
        }
    }
}
